//
//  LXCommonUtils.m
//  LXLessDemo
//
//  Created by LX on 16/2/10.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXCommonUtils.h"

@implementation LXCommonUtils
#pragma mark ===============默认导航栏
+(void)setDefaultTintForNavigationController:(UINavigationController *)navigationController
{
    NSDictionary *attributes;
    if(IOS7_OR_LATER)
    {
        navigationController.navigationBar.translucent = NO;
        navigationController.navigationBar.tintColor = kAppTintColor;
        attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                      kAppTintColor, NSForegroundColorAttributeName,
                      [UIFont boldSystemFontOfSize:18], NSFontAttributeName,nil];
    }
    else
    {
        navigationController.navigationBar.tintColor = kAppTintColor;
        attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                      kAppTintColor, UITextAttributeTextColor,
                      [UIFont boldSystemFontOfSize:18], UITextAttributeFont,nil];
    }
    [navigationController.navigationBar setTitleTextAttributes:attributes];
    [navigationController.navigationBar setTitleTextAttributes: @{NSFontAttributeName:[UIFont systemFontOfSize:18],
                                                                  NSForegroundColorAttributeName:[UIColor whiteColor]}];
    UIImage *navBg = [LXCommonUtils imageWithColor:[UIColor colorWithWhite:0.142 alpha:1.000] size:CGSizeMake(10, 10)];
    UIImage *img = [LXCommonUtils resizableContentImage:navBg edge:UIEdgeInsetsMake(0, 1, 0, 1)];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
+ (CGSize)calculateWithText:(NSString *)text size:(CGSize)size font:(UIFont *)font {
    CGSize expectedLabelSize = CGSizeZero;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
        
        expectedLabelSize = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    }
    else {
        expectedLabelSize = [text sizeWithFont:font
                                    constrainedToSize:size
                                        lineBreakMode:NSLineBreakByWordWrapping];
    }
    
    return CGSizeMake(ceil(expectedLabelSize.width), ceil(expectedLabelSize.height));
}

+ (UIImage*)resizableContentImage:(UIImage*)contentImage edge:(UIEdgeInsets)edge {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
    {
        contentImage = [contentImage resizableImageWithCapInsets:edge resizingMode:UIImageResizingModeStretch];
        
    }else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
    {
        contentImage = [contentImage resizableImageWithCapInsets:edge];
    }
    return contentImage;
}

+(UILabel *)settingLabWithFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *) textColor fount:(UIFont *)fount{
    UILabel *lable = [[UILabel alloc] initWithFrame:frame];
    lable.textColor = textColor;
    lable.backgroundColor = [UIColor clearColor];
    lable.text = text;
    lable.font = fount;
    [lable sizeToFit];
    lable.textAlignment = NSTextAlignmentCenter;
    return lable;
}

@end
